<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="对应酒店ID" prop="sellerId">
      <el-input v-model="dataForm.sellerId" placeholder="对应酒店ID"></el-input>
    </el-form-item>
    <el-form-item label="是否启用 0,关闭 1,启用" prop="switchConfig">
      <el-input v-model="dataForm.switchConfig" placeholder="是否启用 0,关闭 1,启用"></el-input>
    </el-form-item>
    <el-form-item label="绑定支付方式ID" prop="payTypeId">
      <el-input v-model="dataForm.payTypeId" placeholder="绑定支付方式ID"></el-input>
    </el-form-item>
    <el-form-item label="商户号" prop="sellerMerchantNo">
      <el-input v-model="dataForm.sellerMerchantNo" placeholder="商户号"></el-input>
    </el-form-item>
    <el-form-item label="终端号" prop="sellerTerminalId">
      <el-input v-model="dataForm.sellerTerminalId" placeholder="终端号"></el-input>
    </el-form-item>
    <el-form-item label="令牌" prop="sellerToken">
      <el-input v-model="dataForm.sellerToken" placeholder="令牌"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          sellerId: '',
          switchConfig: '',
          payTypeId: '',
          sellerMerchantNo: '',
          sellerTerminalId: '',
          sellerToken: ''
        },
        dataRule: {
          sellerId: [
            { required: true, message: '对应酒店ID不能为空', trigger: 'blur' }
          ],
          switchConfig: [
            { required: true, message: '是否启用 0,关闭 1,启用不能为空', trigger: 'blur' }
          ],
          payTypeId: [
            { required: true, message: '绑定支付方式ID不能为空', trigger: 'blur' }
          ],
          sellerMerchantNo: [
            { required: true, message: '商户号不能为空', trigger: 'blur' }
          ],
          sellerTerminalId: [
            { required: true, message: '终端号不能为空', trigger: 'blur' }
          ],
          sellerToken: [
            { required: true, message: '令牌不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelsellerpayinterfaceconfig/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sellerId = data.tHotelSellerPayInterfaceConfig.sellerId
                this.dataForm.switchConfig = data.tHotelSellerPayInterfaceConfig.switchConfig
                this.dataForm.payTypeId = data.tHotelSellerPayInterfaceConfig.payTypeId
                this.dataForm.sellerMerchantNo = data.tHotelSellerPayInterfaceConfig.sellerMerchantNo
                this.dataForm.sellerTerminalId = data.tHotelSellerPayInterfaceConfig.sellerTerminalId
                this.dataForm.sellerToken = data.tHotelSellerPayInterfaceConfig.sellerToken
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelsellerpayinterfaceconfig/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'switchConfig': this.dataForm.switchConfig,
                'payTypeId': this.dataForm.payTypeId,
                'sellerMerchantNo': this.dataForm.sellerMerchantNo,
                'sellerTerminalId': this.dataForm.sellerTerminalId,
                'sellerToken': this.dataForm.sellerToken
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
